/**
Zeke Elder - zbe@andrew.cmu.edu
Amal Chemmany - achemman@andrew.cmu.edu
Nishant Kumar - nkumar1@andrew.cmu.edu
15-637
4/2/2012
 **/

package edu.cmu.fithub.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import edu.cmu.fithub.controller.Action;
import edu.cmu.fithub.db.DBHelper;
import edu.cmu.fithub.db.bean.GroupMembers;
import edu.cmu.fithub.db.bean.GroupMembershipNotification;
import edu.cmu.fithub.db.bean.User;

public class AcceptInvitesAction extends Action {

	public AcceptInvitesAction() {
	}

	public String getName() {
		return "acceptInvites.do";
	}

	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);
		try {
			User user = (User) request.getSession().getAttribute("user");
			String action = request.getParameter("action");
			String groupId = request.getParameter("groupId");
			Session dbSession = DBHelper.getSessionFactory().openSession();
			if (action != null && action.equalsIgnoreCase("Accept")) {
				// accept Invite i.e. create entry in group Members table for
				// current user.
				List<GroupMembershipNotification> inviteList = dbSession
						.createCriteria(GroupMembershipNotification.class)
						.add(Restrictions.eq("userId", user.getUserId()))
						.add(Restrictions.eq("group.groupId",
								(Integer.parseInt(groupId)))).list();
				if (inviteList.size() != 0) {
					GroupMembers groupMembers = new GroupMembers();
					groupMembers.setGroup(inviteList.get(0).getGroup());
					groupMembers.setUserId(user.getUserId());
					groupMembers.setUserName(user.getFirstName());
					dbSession.beginTransaction();
					dbSession.save(groupMembers);
					dbSession.getTransaction().commit();
					// delete the entry from the notification table
					dbSession.beginTransaction();
					dbSession.delete(inviteList.get(0));
					dbSession.getTransaction().commit();
				} else {
					// TODO: handle invalid group Id sent for accepting invite
					errors.add("Invalid Group!!!");
					return "error.jsp";
				}
			} else if (action != null
					&& action.equalsIgnoreCase("Decline")) {
				List<GroupMembershipNotification> inviteList = dbSession
						.createCriteria(GroupMembershipNotification.class)
						.add(Restrictions.eq("userId", user.getUserId()))
						.add(Restrictions.eq("group.groupId",
								(Integer.parseInt(groupId)))).list();
				if (inviteList.size() != 0) {
					// delete this entry from the notification table
					dbSession.beginTransaction();
					dbSession.delete(inviteList.get(0));
					dbSession.getTransaction().commit();
				} else {
					// TODO: invalid groupID sent to decline invite
					errors.add("Invalid Group!!!");
					return "error.jsp";
				}
			}
		} catch (NumberFormatException e) {
			// TODO error handling
			errors.add("Invalid Group!!!");
			return "error.jsp";
		}

		return "showNotifications.do";
	}
}
